﻿using System;
using System.Collections.Generic;

namespace Xoohoo.Models
{
    public class PagedList<T> : List<T>, IPagedList<T>
    {
        public PagedList(IEnumerable<T> items, int pageIndex, int pageSize
            , int totalItemCount)
        {
            AddRange(items);
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalItemCount = totalItemCount;
            TotalPageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize);
            HasPreviousPage = (PageIndex > 0);
            HasNextPage = (PageIndex < (TotalPageCount - 1));
            IsFirstPage = (PageIndex <= 0);
            IsLastPage = (PageIndex >= (TotalItemCount - 1));
        }

        public PagedList(IEnumerable<T> items, int pageIndex, int pageSize
            , int totalItemCount, int totalPageCount
            , bool hasPreviousPage, bool hasNextPage
            , bool isFirstPage, bool isLastPage
            )
        {
            AddRange(items);
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalItemCount = totalItemCount;
            TotalPageCount = totalPageCount;
            HasPreviousPage = hasPreviousPage;
            HasNextPage = hasNextPage;
            IsFirstPage = isFirstPage;
            IsLastPage = isLastPage;
        }

        #region IPagedList<T> Members

        public int PageNumber { get { return PageIndex + 1; } }
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalItemCount { get; private set; }
        public int TotalPageCount { get; private set; }
        public bool HasPreviousPage { get; private set; }
        public bool HasNextPage { get; private set; }
        public bool IsFirstPage { get; private set; }
        public bool IsLastPage { get; private set; }

        #endregion
    }
}
